from Tkinter import *

## Workstation Status Codes
##   defined here to be available for factory processing
##
statIdle = 0
statSettingUp = 1
statIdleSetup = 2
statProcessing = 3
statHolding = 4
statJobComplete = 5
statBreakdown = 6

## Workstation Colors
##   defined here to be available to process steps
##
wsIdleBackground = 0
wsWorkingBackground = 1
wsForeground = 2

wsColors = (("gainsboro", "gainsboro", "black"),
            ("red4", "red2", "white"),
            ("darkgreen", "green2", "white"),
            ("blue4", "blue2", "white"),
            ("magenta4", "magenta2", "white"),
            ("OrangeRed3", "OrangeRed2", "white"),
            ("DeepSkyBlue4", "DeepSkyBlue2", "white"),
            ("chartreuse4", "chartreuse3", "white"),
            ("gold", "yellow", "black"),
            ("purple4", "MediumPurple4", "white"),
            ("saddle brown", "sienna", "white"),
            ("dark turquoise", "cyan", "black"))


class WorkStation:

  statusCodes = {
      statIdle : "ID",
      statSettingUp : "SU",
      statIdleSetup : "ID",
      statProcessing : "PR",
      statHolding : "HO",
      statJobComplete : "ID",
      statBreakdown : "BR" }

  def __init__(self, master, typeindex, setuptime, stationname,
                     avail, mttf):
    self.frame = Frame(master, width=100, height=60)
    self.frame.pack()
    self.status = statIdle
    self.typeIndex = typeindex
    self.setupTime = setuptime
    self.stationName = stationname
    self.limit = 0
    self.step = 0
    self.availability = avail
    self.MTTF = mttf
    self.StationName = Label(self.frame, text="Station")
    self.StationName.place(relx = 0.5, rely = 0.16, anchor = CENTER)
    self.StatusLabel = Label(self.frame, text="Status")
    self.StatusLabel.place(relwidth = 0.6, relx = 0.3, rely = 0.5, anchor = CENTER)
    self.StatusCode = Label(self.frame, text="--")
    self.StatusCode.place (relwidth = 0.4, relx = 0.8,
                           rely = 0.5, anchor = CENTER)
    self.LimitLabel = Label(self.frame, text="L:")
    self.LimitLabel.place (rely = 0.83, relwidth = 0.2,
                           relx = 0.2, anchor = E )
    self.Limit = Label(self.frame, text = "--")
    self.Limit.place ( rely = 0.83, relwidth = 0.2, relx = 0.2, anchor = W)
    self.Step = Label(self.frame, text="--")
    self.Step.place (rely = 0.5, relwidth = 0.4, relx = 0.8, anchor = CENTER)
    self.SetupTime = Label(self.frame, text = "0")
    self.SetupTime.place(rely= 0.83, relwidth = 0.4, relx = 0.8, anchor = CENTER)
    self.redraw()
    self.setcolor()

  def redraw(self):
    self.StationName.config(text=self.stationName)
    self.StatusCode.config(text=self.statusCodes[self.status])
    limittext = "--" if self.limit == 0 else "{0}".format(self.limit)
    self.Limit.config(text = limittext)
    steptext = "--" if self.step == 0 else "{0}".format(self.step)
    self.Step.config(text = steptext)
    self.SetupTime.config(text = "{0}".format(self.setupTime))

  def setcolor(self):
    bgindex = wsWorkingBackground if self.status == statProcessing else wsIdleBackground
    bgcolor = wsColors[self.typeIndex][bgindex]
    fgcolor = wsColors[self.typeIndex][wsForeground]
    self.frame.configure (bg = bgcolor)
    for child in self.frame.children:
      self.frame.children[child].configure (bg = bgcolor,
                                              fg = fgcolor)

  def setStatus(self, status):
    self.status = status


